package com.tinyseed.emcp.api.dashboard.rest;

import com.tinyseed.emcp.api.dashboard.service.DashboardService;
import com.tinyseed.emcp.api.exception.entity.ExceptionInfo;
import com.tinyseed.emcp.api.exception.service.ExceptionInfoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "系统：dashboard")
@RestController
@Slf4j
@RequestMapping("/api/dashboard")
public class DashboardController {

    private final DashboardService dashboardService;

    private final ExceptionInfoService exceptionInfoService;

    public DashboardController(DashboardService dashboardService, ExceptionInfoService infoService) {
        this.dashboardService = dashboardService;
        this.exceptionInfoService = infoService;
    }

    @GetMapping(value = "/basicInfo")
    public ResponseEntity basicInfo(){
        return new ResponseEntity<>(dashboardService.basicInfo(), HttpStatus.OK);
    }

    @GetMapping(value = "/limitState")
    public ResponseEntity limitState(){
        return new ResponseEntity<>(dashboardService.computeEnterpriseStatue(), HttpStatus.OK);
    }

    @GetMapping(value = "/yesterdayLimitState")
    public ResponseEntity yesterdayLimitState(){
        return new ResponseEntity<>(dashboardService.computeEnterpriseStatue(), HttpStatus.OK);
    }

    @GetMapping(value = "/todayExceptionInfo")
    public ResponseEntity todayExceptionInfo(){
        //获取当天的异常信息
        return new ResponseEntity<>(exceptionInfoService.getDailyException(), HttpStatus.OK);
    }

    @GetMapping(value = "/yesterdayExceptionInfo")
    public ResponseEntity yesterdayExcepioninfo(){
        return new ResponseEntity<>(exceptionInfoService.getYesterdayExceptionInfo(), HttpStatus.OK);
    }


    @GetMapping(value = "/todayMonitorExceptionInfo")
    public ResponseEntity todayMonitorExceptionInfo(){
        return new ResponseEntity<>(exceptionInfoService.getMonitorMapByExceptionInfo(), HttpStatus.OK);
    }


    @GetMapping(value = "/organizeException")
    public ResponseEntity organizeException(){

        Map<String, Collection<ExceptionInfo>> temp = exceptionInfoService.getOrganizeGroupByExceptionInfos().asMap();

        HashMap<String,Integer> organizeExceptionMap = new HashMap<>();

        temp.forEach((key,value)->{

            organizeExceptionMap.put(key,value.size());

        });
        return new ResponseEntity<>(organizeExceptionMap, HttpStatus.OK);
    }




}
